package com.chx.three.mama.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.chx.three.mama.R;
import com.chx.three.mama.model.RingItem;
import com.chx.three.mama.utils.BitmapHelper;
import com.chx.three.mama.utils.ImageLoader;
import com.chx.three.mama.utils.SPHelper;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.core.BitmapCache;


/***
 * 自定义适配器
 *
 * @author zhangjia
 *
 */
public class DragListAdapter extends BaseAdapter {
    private ArrayList<RingItem> arrayTitles;
    private Context context;
    private ArrayList<Integer> sort;
    public boolean isHidden;
    public DragListAdapter(Context context, ArrayList<RingItem> arrayTitles) {
        this.context = context;
        this.arrayTitles = arrayTitles;
    }


    public void showDropItem(boolean showItem){
        this.ShowItem = showItem;
    }

    public void setInvisiblePosition(int position){
        invisilePosition = position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        /***
         * 在这里尽可能每次都进行实例化新的，这样在拖拽ListView的时候不会出现错乱.
         * 具体原因不明，不过这样经过测试，目前没有发现错乱。虽说效率不高，但是做拖拽LisView足够了。
         */
        convertView = LayoutInflater.from(context).inflate(R.layout.item_ring_list_item,null);

        TextView title = (TextView) convertView.findViewById(R.id.ring_list__item_title);
        TextView desc =(TextView)convertView.findViewById(R.id.ring_list__item_desc);
        ImageView image=(ImageView)convertView.findViewById(R.id.ring_list__item_image);
        ImageView icon1=(ImageView)convertView.findViewById(R.id.ring_list__item_icon1);
        ImageView icon2=(ImageView)convertView.findViewById(R.id.ring_list__item_icon2);
        ImageView icon3=(ImageView)convertView.findViewById(R.id.ring_list__item_icon3);
        ImageView icon4=(ImageView)convertView.findViewById(R.id.ring_list__item_icon4);
        ImageView icon5=(ImageView)convertView.findViewById(R.id.ring_list__item_icon5);
        RelativeLayout layout=(RelativeLayout)convertView.findViewById(R.id.ring_list_item_sort);

        RingItem ringItem = arrayTitles.get(position);
        if(ringItem.isSorting()){
            layout.setVisibility(View.VISIBLE);
        }else{
            layout.setVisibility(View.GONE);

        }

        desc.setText(ringItem.getNew_post());
        title.setText(ringItem.getName());
        BitmapUtils bitmapUtils = BitmapHelper.getBitmapUtils();
        bitmapUtils.display(image, ringItem.getPic());
        String[] avatar = ringItem.getAvatar();
        bitmapUtils.display(icon1,avatar[0]);
        bitmapUtils.display(icon2,avatar[1]);
        bitmapUtils.display(icon3,avatar[2]);
        bitmapUtils.display(icon4,avatar[3]);
        bitmapUtils.display(icon5,avatar[4]);


        if (isChanged){
            if (position == invisilePosition){
                if(!ShowItem){
                    convertView.setVisibility(View.INVISIBLE);
                }
            }
            if(lastFlag != -1){
                if(lastFlag == 1){
                    if(position > invisilePosition){
                        Animation animation;
                        animation = getFromSelfAnimation(0, -height);
                        convertView.startAnimation(animation);
                    }
                }else if(lastFlag == 0){
                    if(position < invisilePosition){
                        Animation animation;
                        animation = getFromSelfAnimation(0, height);
                        convertView.startAnimation(animation);
                    }
                }
            }
        }
        return convertView;
    }

    /***
     * 动态修改ListVIiw的方位.
     *
     * @param start
     *            点击移动的position
     * @param down
     *            松开时候的position
     */
    private int invisilePosition = -1;
    private boolean isChanged = true;
    private boolean ShowItem = false;

    public void exchange(int startPosition, int endPosition) {
        System.out.println(startPosition + "--" + endPosition);
        Object startObject = getItem(startPosition);
        if(startPosition < endPosition){
            arrayTitles.add(endPosition + 1, (RingItem) startObject);
            arrayTitles.remove(startPosition);
        }else{
            arrayTitles.add(endPosition,(RingItem)startObject);
            arrayTitles.remove(startPosition + 1);
        }

        isChanged = true;
    }

    public void exchangeCopy(int startPosition, int endPosition) {
        Object startObject = getCopyItem(startPosition);
        if(startPosition < endPosition){
            mCopyList.add(endPosition + 1, (RingItem) startObject);
    mCopyList.remove(startPosition);
}else{
        mCopyList.add(endPosition,(RingItem)startObject);
        mCopyList.remove(startPosition + 1);
        }
        setSort(startPosition,endPosition);
        isChanged = true;
        }
    private void setSort(int startPosition,int endPosition) {
        sort=new ArrayList<>();
        String listSort = SPHelper.getRingListSort();
        String[] split = listSort.split(",");
        for (int i = 0; i < split.length; i++) {
            if(!TextUtils.isEmpty(split[i])){
                sort.add(i, Integer.parseInt(split[i]));
            }
        }

        int temp=sort.get(startPosition);
        sort.add(startPosition, sort.get(endPosition));
        sort.remove(startPosition+1);
        sort.add(endPosition,temp);
        sort.remove(endPosition+1);

        StringBuilder sb=new StringBuilder();
        for (int i = 0; i < sort.size(); i++) {
            sb.append(sort.get(i));
            sb.append(",");
        }
        SPHelper.setRingListSort(sb.toString());
    }


    public Object getCopyItem(int position) {
        return mCopyList.get(position);
    }

    @Override
    public int getCount() {
        return arrayTitles.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayTitles.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

//    public void addDragItem(int start, Object obj){
//        String title = arrayTitles.get(start);
//        arrayTitles.remove(start);// 删除该项
//        arrayTitles.add(start, (String)obj);// 添加删除项
//    }

    private ArrayList<RingItem> mCopyList = new ArrayList<RingItem>();

    public void copyList(){
        mCopyList.clear();
        for (RingItem str : arrayTitles) {
            mCopyList.add(str);
        }
    }

    public void pastList(){

        arrayTitles.clear();
        for (RingItem str : mCopyList) {
            arrayTitles.add(str);

        }
    }

    private boolean isSameDragDirection = true;
    private int lastFlag = -1;
    private int height;
    private int dragPosition = -1;

    public void setIsSameDragDirection(boolean value){
        isSameDragDirection = value;
    }

    public void setLastFlag(int flag){
        lastFlag = flag;
    }

    public void setHeight(int value){
        height = value;
    }

    public void setCurrentDragPosition(int position){
        dragPosition = position;
    }

    public Animation getFromSelfAnimation(int x,int y){
        TranslateAnimation go = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.ABSOLUTE, x,
                Animation.RELATIVE_TO_SELF, 0, Animation.ABSOLUTE, y);
        go.setInterpolator(new AccelerateDecelerateInterpolator());
        go.setFillAfter(true);
        go.setDuration(100);
        go.setInterpolator(new AccelerateInterpolator());
        return go;
    }

    public Animation getToSelfAnimation(int x,int y){
        TranslateAnimation go = new TranslateAnimation(
                Animation.ABSOLUTE, x, Animation.RELATIVE_TO_SELF, 0,
                Animation.ABSOLUTE, y, Animation.RELATIVE_TO_SELF, 0);
        go.setInterpolator(new AccelerateDecelerateInterpolator());
        go.setFillAfter(true);
        go.setDuration(100);
        go.setInterpolator(new AccelerateInterpolator());
        return go;
    }
}